﻿//-----------------------------------------------------------------------
// <copyright file="Size2D.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Common.Math
{
    using System;

    /// <summary>
    /// 2D size structure.
    /// </summary>
    public struct Size2D
    {
        #region Fields - Public

        /// <summary>
        /// Size width value.
        /// </summary>
        public float width;

        /// <summary>
        /// Size height value.
        /// </summary>
        public float height;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor which takes a single value which is
        /// used as width and height..
        /// </summary>
        /// <param name="wh">Size width and height.</param>
        public Size2D(float wh)
        {
            if (wh < 0.0f)
            {
                throw new ArgumentException("Width must be >= 0.0f");
            }

            this.width = wh;
            this.height = wh;
        }

        /// <summary>
        /// Constructor which takes a width and height.
        /// </summary>
        /// <param name="w">Size width.</param>
        /// <param name="h">Size height.</param>
        public Size2D(float w, float h)
        {
            if (w < 0.0f)
            {
                throw new ArgumentException("Width must be >= 0.0f");
            }

            if (h < 0.0f)
            {
                throw new ArgumentException("Height must be >= 0.0f");
            }

            this.width = w;
            this.height = h;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets or sets the width of the rectangle.
        /// </summary>
        public float Width
        {
            get
            {
                return this.width;
            }

            set
            {
                this.width = value;
            }
        }

        /// <summary>
        /// Gets or sets the height of the rectangle.
        /// </summary>
        public float Height
        {
            get
            {
                return this.height;
            }

            set
            {
                this.height = value;
            }
        }

        /// <summary>
        /// Gets a size object which is half the size of the current size.
        /// </summary>
        public Size2D Half
        {
            get
            {
                return new Size2D(this.Width * 0.5f, this.Height * 0.5f);
            }
        }

        #endregion

        #region Operator overloads

        /// <summary>
        /// Multiply a size by a float.
        /// </summary>
        /// <param name="lhs">Left hand side size.</param>
        /// <param name="rhs">Right hand side float.</param>
        /// <returns>Returns lhs - rhs.</returns>
        public static Size2D operator *(Size2D lhs, float rhs)
        {
            return new Size2D(lhs.width * rhs, lhs.height * rhs);
        }

        /// <summary>
        /// Add two sizes.
        /// </summary>
        /// <param name="lhs">Left hand side size.</param>
        /// <param name="rhs">Right hand side size.</param>
        /// <returns>Returns lhs - rhs.</returns>
        public static Size2D operator +(Size2D lhs, Size2D rhs)
        {
            return new Size2D(lhs.width + rhs.width, lhs.height + rhs.height);
        }

        #endregion

        #region Methods - Public static

        /// <summary>
        /// Attempt to parse a size from the passed string.
        /// </summary>
        /// <param name="value">String to parse a size from.</param>
        /// <param name="size">Destination size.</param>
        /// <returns>Returns true if the size was parsed successfully.</returns>
        public static bool TryParse(string value, out Size2D size)
        {
            size = new Size2D(0.0f, 0.0f);

            if (string.IsNullOrEmpty(value))
            {
                return false;
            }

            string[] parts = value.Split(' ');
            if (parts == null || parts.Length != 2)
            {
                return false;
            }

            bool success = true;

            success &= float.TryParse(parts[0], out size.width);
            success &= float.TryParse(parts[1], out size.height);

            return success;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Convert object to string.
        /// </summary>
        /// <returns>Returns a string representation of the object.</returns>
        public override string ToString()
        {
            return string.Format("{0} {1}", this.Width, this.Height);
        }

        #endregion
    }
}
